package cn.edu.cdtu.healthserver.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cdtu.healthserver.domain.HealthNewsJson;
import cn.edu.cdtu.healthserver.service.HealthNewsService;
import cn.edu.cdtu.healthserver.service.impl.HealthNewsServiceImplementation;
import cn.edu.cdtu.healthserver.utils.ConnectUtil;

import com.google.gson.Gson;

/**
 * Servlet implementation class HealthNewsServlet
 */
@WebServlet("/HealthNewsServlet")
public class HealthNewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private HealthNewsService healthNewsService =
			new HealthNewsServiceImplementation();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HealthNewsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		int id = Integer.parseInt(request.getParameter("id"));
		String operation = request.getParameter(
				ConnectUtil.HealthNewsPargrams.Option.HEALTH_NEWS_OPERATION);
		HealthNewsJson healthNewsJson = healthNewsService.queryHealthNews(id, operation);
		Gson gson = new Gson();
		String healthNewsJsonString = gson.toJson(healthNewsJson);
		response.setContentType(ConnectUtil.ContentTypeString.jSON_STRING);
		PrintWriter writer = response.getWriter();
		writer.write(healthNewsJsonString);
		writer.flush();
		writer.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
